package com.triptogether.baseclasses;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class TripTogetherDialog extends DialogFragment {
	
	private static final String TITLE = "TITLE";
	private static final String MESSAGE = "MESSAGE";
	private static final String POSITIVE_BUTTON = "POSITIVE_BUTTON";
	private static final String NEGATIVE_BUTTON = "NEGATIVE_BUTTON";

	protected String title;
	protected String message;
	protected String positiveButton;
	protected String negativeButton;
	private TripTogetherDialogListener tripTogetherDialogListener;

	public TripTogetherDialog(String title, String message, String positiveButton, String negativeButton) {
		this.title = title;
		this.message = message;
		this.positiveButton = positiveButton;
		this.negativeButton = negativeButton;
	}

	public TripTogetherDialog(String title, String message, String positiveButton) {
		this(title, message, positiveButton, null);
	}

	public TripTogetherDialog(String title, String message) {
		this(title, message, null);
	}

	public TripTogetherDialog(int titleId, int messageId, int positiveButtonId, int negativeButtonId) {
		this.title = getString(titleId);
		this.message = getString(messageId);
		this.positiveButton = getString(positiveButtonId);
		this.negativeButton = getString(negativeButtonId);
	}

	public TripTogetherDialog(int titleId, int messageId, int positiveButtonId) {
		this.title = getString(titleId);
		this.message = getString(messageId);
		this.positiveButton = getString(positiveButtonId);
		this.negativeButton = null;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(TITLE, title);
		outState.putString(MESSAGE, message);
		outState.putString(POSITIVE_BUTTON, positiveButton);
		outState.putString(NEGATIVE_BUTTON, negativeButton);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    // Get the parameters of this DialogFragment from the savedInstanceState
	    if (savedInstanceState != null) {
    		this.title = savedInstanceState.getString(TITLE);
    		this.message = savedInstanceState.getString(MESSAGE);
    		this.positiveButton = savedInstanceState.getString(POSITIVE_BUTTON);
    		this.negativeButton = savedInstanceState.getString(NEGATIVE_BUTTON);
	    }
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
		dialogBuilder.setTitle(title);
		dialogBuilder.setMessage(message);
		if (positiveButton != null) {
			dialogBuilder.setPositiveButton(positiveButton, new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (tripTogetherDialogListener != null) {
						tripTogetherDialogListener.onPositiveButtonClicked();
					}
				}
			});
		}
		if (negativeButton != null) {
			dialogBuilder.setNegativeButton(negativeButton, new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (tripTogetherDialogListener != null) {
						tripTogetherDialogListener.onNegativeButtonClicked();
					}
				}
			});
		}
		
		return dialogBuilder.create();
	}

	// //////////////////////////////////
	// Listener to dialog
	// //////////////////////////////////
	
	public interface TripTogetherDialogListener {
		public void onPositiveButtonClicked();
		public void onNegativeButtonClicked();
	}
	
	public void setTripTogetherDialogListener(TripTogetherDialogListener tripTogetherDialogListener) {
		this.tripTogetherDialogListener = tripTogetherDialogListener;
	}
	
	// //////////////////////////////////
	// Listener to dialog
	// //////////////////////////////////
}
